﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Sispe.UI
{
    public partial class FrmMttoContactoProyecto : Form
    {
        [Dependency]
        public IContactoProyectoBL contactoProyectoBL { get; set; }

        public FrmMttoContactoProyecto()
        {
            InitializeComponent();
            this.BuildUp();
        }

        bool isNew;

        public void NuevoContacto()
        {
            isNew = true;
            cONTACTO_PROYECTOBindingSource.AddNew();
        }

        public CONTACTO_PROYECTO _contacto { get; set; }

        public void EditarContacto()
        {
            isNew = false;
            cONTACTO_PROYECTOBindingSource.DataSource = _contacto;
        }

        private void FrmMttoContactoProyecto_Load(object sender, EventArgs e)
        {

        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            var contacto = cONTACTO_PROYECTOBindingSource.Current as CONTACTO_PROYECTO;

            var msg = "ERROR";

            if (contacto != null)
            {
                if (isNew == true)
                {
                    try
                    {
                        contacto.Nombre = nombreTextBox.Text.ToUpper();
                        contacto.Email = emailTextBox.Text.ToUpper();
                        contacto.Telefono = telefonoTextBox.Text.ToUpper();
                        contactoProyectoBL.AddContactoProyecto(contacto);

                        MessageBox.Show("Registro Correcto", "Registro", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (isNew == false)
                {
                    try
                    {
                        contacto.Nombre = nombreTextBox.Text.ToUpper();
                        contacto.Email = emailTextBox.Text.ToUpper();
                        contacto.Telefono = telefonoTextBox.Text.ToUpper();
                        contactoProyectoBL.UpdateContactoProyecto(contacto);

                        MessageBox.Show("Modificación Correcta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void nombreTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void telefonoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        public int VecesArroba = 0;
        private void emailTextBox_KeyPress(object sender, KeyPressEventArgs e)
         {
            var guion = Convert.ToString("_");
            var guion2 = Convert.ToString("-");
            var punto = Convert.ToString(".");

            string cadena = guion + guion2 + punto + (char)8;
            string arrobaa = Convert.ToString("@") + (char)8;

            if (arrobaa.Contains(e.KeyChar))
            {
                VecesArroba++;

                if (VecesArroba == 1)
                {
                    if (!arrobaa.Contains(e.KeyChar) && !(char.IsNumber(e.KeyChar)) && !(char.IsLetterOrDigit(e.KeyChar)) && (e.KeyChar != (char)Keys.Back)
                        && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear))
                    {
                        e.Handled = true; 
                        return;
                    }
                }
                else
                {
                    if (!cadena.Contains(e.KeyChar) &&  !(char.IsNumber(e.KeyChar)) && !(char.IsLetterOrDigit(e.KeyChar)) && (e.KeyChar != (char)Keys.Back)
                        && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear))
                    {
                        e.Handled = true; return;
                    }
                }
            }
            else
            {
                if (!cadena.Contains(e.KeyChar) &&  !(char.IsNumber(e.KeyChar)) && !(char.IsLetterOrDigit(e.KeyChar)) && (e.KeyChar != (char)Keys.Back)
                        && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear))
                {
                    e.Handled = true;
                    return;
                }
            }

        }
    }
}
